package com.yunshipei.base;

import android.text.TextUtils;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

public class BaseSubscriberAdapter<T> implements Subscriber<T> {

    @Override
    public void onError(Throwable e) {
        String exception = e.toString();
        if (!TextUtils.isEmpty(exception)) {
            String temp = exception.toLowerCase();
            if (temp.contains("time") && temp.contains("out")) {
                exception = "请求超时，请重试...";
            } else if (temp.contains("json") && temp.contains("exception")) {
                exception = "数据解析失败...";
            } else if (temp.contains("unexpected end of stream") || temp.contains("ioexception")) {
                exception = "链接异常终止，请重试...";
            } else if (temp.contains("unknownhostexception")) {
                exception = "未知服务器地址...";
            } else if (temp.contains("httpexception")) {
                exception = "Http请求异常:" + e.getMessage();
            }
        } else {
            exception = "未知错误";
        }
        onError(exception);
    }

    @Override
    public void onComplete() {

    }

    public void onError(String errorMessage) {

    }

    @Override
    public void onSubscribe(Subscription s) {
        s.request(Long.MAX_VALUE);
    }

    @Override
    public void onNext(T t) {

    }
}
